package myContacts.wrapper;

import java.util.ArrayList;
import java.util.List;

import myContacts.AdditionalName;
import myContacts.Agent;
import myContacts.City;
import myContacts.Contact;
import myContacts.Country;
import myContacts.FamilyName;
import myContacts.FormattedAddress;
import myContacts.FullName;
import myContacts.GivenName;
import myContacts.Housename;
import myContacts.NamePrefix;
import myContacts.NameSuffix;
import myContacts.Neighborhood;
import myContacts.Nickname;
import myContacts.Occupation;
import myContacts.PoBox;
import myContacts.PostCode;
import myContacts.Region;
import myContacts.Street;
import myContacts.Subregion;


import com.google.gdata.data.Link;
import com.google.gdata.data.contacts.ContactEntry;
import com.google.gdata.data.extensions.Email;
import com.google.gdata.data.extensions.HouseName;
import com.google.gdata.data.extensions.Name;
import com.google.gdata.data.extensions.PhoneNumber;
import com.google.gdata.data.extensions.StructuredPostalAddress;



public class ContactWrapper {

	public static Contact getContact(ContactEntry contactEntry) {

		Contact contact = new Contact();

		contact.setId(contactEntry.getId());
		contact.setVersionId(contactEntry.getVersionId());
		contact.setEmailAddresses(wrappGoogleEmailAddresses(contactEntry.getEmailAddresses()));
		contact.setContactPhotoLink(wrappGoogleLink(contactEntry.getContactPhotoLink()));
		
		if(contactEntry.getName()!=null)
			contact.setName(wrappGoogleName(contactEntry.getName()));
		else
			contact.setName(null);
		
		if(contactEntry.getNickname()!=null)
			contact.setNickname(new Nickname(contactEntry.getNickname().getValue()));
		else
			contact.setNickname(null);
		
		if(contactEntry.getOccupation()!=null)
			contact.setOccupation(new Occupation(contactEntry.getOccupation().getValue()));
		else
			contact.setOccupation(null);
		
		contact.setPhoneNumbers(wrappGooglePhoneNunbers(contactEntry.getPhoneNumbers()));
		contact.setStructuredPostalAddresses(wrappGoogleStructuredPostalAddresses(contactEntry.getStructuredPostalAddresses()));
		contact.setEtag(contactEntry.getEtag());
		contact.setEditLink(wrappGoogleLink(contactEntry.getEditLink()));

		return contact;
	}

	private static List<myContacts.Email> wrappGoogleEmailAddresses(List<Email> emailAddresses) {

		List<myContacts.Email> emails = new ArrayList<myContacts.Email>();

		for (Email email : emailAddresses) {

			myContacts.Email myEmail = new myContacts.Email();

			myEmail.setAddress(email.getAddress());
			myEmail.setDisplayName(email.getDisplayName());
			myEmail.setLabel(email.getLabel());
			myEmail.setPrimary(email.getPrimary());
			myEmail.setQuota(email.getQuota());
			myEmail.setRel(email.getRel());

			emails.add(myEmail);
		}

		return emails;
	}
	
	public static Link wrappMyLink(myContacts.Link link) {

		Link googleLink = new Link();

		googleLink.setEtag(link.getEtag());
		googleLink.setHref(link.getHref());
		googleLink.setHrefLang(link.getHrefLang());
		googleLink.setLength(link.getLength());
		googleLink.setRel(link.getRel());
		googleLink.setTitle(link.getTitle());
		googleLink.setTitleLang(link.getTitleLang());
		googleLink.setType(link.getType());

		return googleLink;
	}

	private static myContacts.Link wrappGoogleLink(Link link) {

		myContacts.Link myLink = new myContacts.Link();

		myLink.setEtag(link.getEtag());
		myLink.setHref(link.getHref());
		myLink.setHrefLang(link.getHrefLang());
		myLink.setLength(link.getLength());
		myLink.setRel(link.getRel());
		myLink.setTitle(link.getTitle());
		myLink.setTitleLang(link.getTitleLang());
		myLink.setType(link.getType());

		return myLink;
	}

	private static myContacts.Name wrappGoogleName(Name name) {

		myContacts.Name myName = new myContacts.Name();

		if(name.getAdditionalName()!=null)
			myName.setAdditionalName(new AdditionalName(name.getAdditionalName().getValue(), name.getAdditionalName().getYomi()));
		else
			myName.setAdditionalName(null);
		
		if(name.getFamilyName()!=null)
			myName.setFamilyName(new FamilyName(name.getFamilyName().getValue(), name.getFamilyName().getYomi()));
		else
			myName.setFamilyName(null);
		
		if(name.getFullName()!=null)
			myName.setFullName(new FullName(name.getFullName().getValue(), name.getFullName().getYomi()));
		else
			myName.setFullName(null);
		
		if(name.getGivenName()!=null)
			myName.setGivenName(new GivenName(name.getGivenName().getValue(), name.getGivenName().getYomi()));
		else
			myName.setGivenName(null);
		
		if(name.getNamePrefix()!=null)
			myName.setNamePrefix(new NamePrefix(name.getNamePrefix().getValue()));
		else
			myName.setNamePrefix(null);
		
		if(name.getNameSuffix()!=null)
			myName.setNameSuffix(new NameSuffix(name.getNameSuffix().getValue()));
		else
			myName.setNameSuffix(null);

		return myName;
	}
	
	private static List<myContacts.PhoneNumber> wrappGooglePhoneNunbers(List<PhoneNumber> phoneNumbers) {

		List<myContacts.PhoneNumber> myPhoneNumbers = new ArrayList<myContacts.PhoneNumber>();

		for (PhoneNumber phoneNumber : phoneNumbers) {

			myContacts.PhoneNumber myPhoneNumber = new myContacts.PhoneNumber();

			myPhoneNumber.setLabel(phoneNumber.getLabel());
			myPhoneNumber.setPhoneNumber(phoneNumber.getPhoneNumber());
			myPhoneNumber.setPrimary(phoneNumber.getPrimary());
			myPhoneNumber.setRel(phoneNumber.getRel());
			myPhoneNumber.setUri(phoneNumber.getUri());
			
			myPhoneNumbers.add(myPhoneNumber);
		}

		return myPhoneNumbers;
	}
	
	private static List<myContacts.StructuredPostalAddress> wrappGoogleStructuredPostalAddresses(List<StructuredPostalAddress> structuredPostalAddresses) {

		List<myContacts.StructuredPostalAddress> myStructuredPostalAddresses = new ArrayList<myContacts.StructuredPostalAddress>();

		for (StructuredPostalAddress structuredPostalAddress : structuredPostalAddresses) {

			myContacts.StructuredPostalAddress myStructuredPostalAddress = new myContacts.StructuredPostalAddress();

			if(structuredPostalAddress.getAgent()!=null)
				myStructuredPostalAddress.setAgent(new Agent(structuredPostalAddress.getAgent().getValue()));
			else
				myStructuredPostalAddress.setAgent(null);
			
			if(structuredPostalAddress.getCity()!=null)
				myStructuredPostalAddress.setCity(new City(structuredPostalAddress.getCity().getValue(), structuredPostalAddress.getCity().isImmutable()));
			else
				myStructuredPostalAddress.setCity(null);
			
			if(structuredPostalAddress.getCountry()!=null)
				myStructuredPostalAddress.setCountry(new Country(structuredPostalAddress.getCountry().getCode(), structuredPostalAddress.getCountry().getValue(), structuredPostalAddress.getCountry().isImmutable()));
			else
				myStructuredPostalAddress.setCountry(null);
			
			if(myStructuredPostalAddress.getFormattedAddress()!=null)
				myStructuredPostalAddress.setFormattedAddress(new FormattedAddress(structuredPostalAddress.getFormattedAddress().getValue()));
			else
				myStructuredPostalAddress.setFormattedAddress(null);
			
			if(structuredPostalAddress.getHousename()!=null)
				myStructuredPostalAddress.setHousename(new Housename(structuredPostalAddress.getHousename().getValue()));
			else
				myStructuredPostalAddress.setHousename(null);
			
			myStructuredPostalAddress.setLabel(structuredPostalAddress.getLabel());
			
			myStructuredPostalAddress.setMailClass(structuredPostalAddress.getMailClass());
			
			if(structuredPostalAddress.getNeighborhood()!=null)
				myStructuredPostalAddress.setNeighborhood(new Neighborhood(structuredPostalAddress.getNeighborhood().getValue(), structuredPostalAddress.getNeighborhood().isImmutable()));
			else
				myStructuredPostalAddress.setNeighborhood(null);
			
			if(structuredPostalAddress.getPobox()!=null)
				myStructuredPostalAddress.setPobox(new PoBox(structuredPostalAddress.getPobox().getValue()));
			else
				myStructuredPostalAddress.setPobox(null);
			
			if(structuredPostalAddress.getPostcode()!=null)
				myStructuredPostalAddress.setPostcode(new PostCode(structuredPostalAddress.getPostcode().getValue(), structuredPostalAddress.getPostcode().isImmutable()));
			else
				myStructuredPostalAddress.setPostcode(null);
			
			myStructuredPostalAddress.setPrimary(structuredPostalAddress.getPrimary());
			
			if(structuredPostalAddress.getRegion()!=null)
				myStructuredPostalAddress.setRegion(new Region(structuredPostalAddress.getRegion().getValue(), structuredPostalAddress.getRegion().isImmutable()));
			else
				myStructuredPostalAddress.setRegion(null);
			
			myStructuredPostalAddress.setRel(structuredPostalAddress.getRel());
			
			if(structuredPostalAddress.getStreet()!=null)
				myStructuredPostalAddress.setStreet(new Street(structuredPostalAddress.getStreet().getValue()));
			else
				myStructuredPostalAddress.setStreet(null);
			
			if(structuredPostalAddress.getSubregion()!=null)
				myStructuredPostalAddress.setSubregion(new Subregion(structuredPostalAddress.getSubregion().getValue()));
			else
				myStructuredPostalAddress.setSubregion(null);
			
			myStructuredPostalAddress.setUsage(structuredPostalAddress.getUsage());
			
			myStructuredPostalAddresses.add(myStructuredPostalAddress);			
		}

		return myStructuredPostalAddresses;
	}	
	
	public static ContactEntry getContactEntry(Contact contact) {

		ContactEntry contactEntry = new ContactEntry();
		
		contactEntry.setId(contact.getId());
		contactEntry.setVersionId(contact.getVersionId());
		contactEntry.getEmailAddresses().addAll(wrappMyEmailAddresses(contact.getEmailAddresses()));
		
//		contactEntry.getContactPhotoLink().setEtag(contact.getContactPhotoLink().getEtag());
//		contactEntry.getContactPhotoLink().setHref(contact.getContactPhotoLink().getHref());
//		contactEntry.getContactPhotoLink().setHrefLang(contact.getContactPhotoLink().getHrefLang());
//		contactEntry.getContactPhotoLink().setLength(contact.getContactPhotoLink().getLength());
//		contactEntry.getContactPhotoLink().setRel(contact.getContactPhotoLink().getRel());
//		contactEntry.getContactPhotoLink().setTitle(contact.getContactPhotoLink().getTitle());
//		contactEntry.getContactPhotoLink().setTitleLang(contact.getContactPhotoLink().getTitleLang());
//		contactEntry.getContactPhotoLink().setType(contact.getContactPhotoLink().getType());
		
		if(contact.getName()!=null)
			contactEntry.setName(wrappMyName(contact.getName()));
		else
			contactEntry.setName(null);
		
		if(contact.getNickname()!=null)
			contactEntry.setNickname(new com.google.gdata.data.contacts.Nickname(contact.getNickname().getValue()));
		else
			contactEntry.setNickname(null);
		
		if(contact.getOccupation()!=null)
			contactEntry.setOccupation(new com.google.gdata.data.contacts.Occupation(contact.getOccupation().getValue()));
		else
			contactEntry.setOccupation(null);
		
		contactEntry.getPhoneNumbers().addAll(wrappMyPhoneNunbers(contact.getPhoneNumbers()));
		contactEntry.getStructuredPostalAddresses().addAll(wrappMyStructuredPostalAddresses(contact.getStructuredPostalAddresses()));
		contactEntry.setEtag(contact.getEtag());
		
//		contactEntry.getEditLink().setEtag(contact.getEditLink().getEtag());
//		contactEntry.getEditLink().setHref(contact.getEditLink().getHref());
//		contactEntry.getEditLink().setHrefLang(contact.getEditLink().getHrefLang());
//		contactEntry.getEditLink().setLength(contact.getEditLink().getLength());
//		contactEntry.getEditLink().setRel(contact.getEditLink().getRel());
//		contactEntry.getEditLink().setTitle(contact.getEditLink().getTitle());
//		contactEntry.getEditLink().setTitleLang(contact.getEditLink().getTitleLang());
//		contactEntry.getEditLink().setType(contact.getEditLink().getType());

		return contactEntry;
	}

	private static List<Email> wrappMyEmailAddresses(List<myContacts.Email> emailAddresses) {

		List<Email> emails = new ArrayList<Email>();

		for (myContacts.Email email : emailAddresses) {

			Email googleEmail = new Email();

			googleEmail.setAddress(email.getAddress());
			googleEmail.setDisplayName(email.getDisplayName());
			googleEmail.setLabel(email.getLabel());
			googleEmail.setPrimary(email.isPrimary());
			googleEmail.setQuota(email.getQuota());
			googleEmail.setRel(email.getRel());

			emails.add(googleEmail);
		}

		return emails;
	}

	private static Name wrappMyName(myContacts.Name name) {

		Name googleName = new Name();

		if(name.getAdditionalName()!=null)
			googleName.setAdditionalName(new com.google.gdata.data.extensions.AdditionalName(name.getAdditionalName().getValue(), name.getAdditionalName().getYomi()));
		else
			googleName.setAdditionalName(null);
		
		if(name.getFamilyName()!=null)
			googleName.setFamilyName(new com.google.gdata.data.extensions.FamilyName(name.getFamilyName().getValue(), name.getFamilyName().getYomi()));
		else
			googleName.setFamilyName(null);
		
		if(name.getFullName()!=null)
			googleName.setFullName(new com.google.gdata.data.extensions.FullName(name.getFullName().getValue(), name.getFullName().getYomi()));
		else
			googleName.setFullName(null);
		
		if(name.getGivenName()!=null)
			googleName.setGivenName(new com.google.gdata.data.extensions.GivenName(name.getGivenName().getValue(), name.getGivenName().getYomi()));
		else
			googleName.setGivenName(null);
		
		if(name.getNamePrefix()!=null)
			googleName.setNamePrefix(new com.google.gdata.data.extensions.NamePrefix(name.getNamePrefix().getValue()));
		else
			googleName.setNamePrefix(null);
		
		if(name.getNameSuffix()!=null)
			googleName.setNameSuffix(new com.google.gdata.data.extensions.NameSuffix(name.getNameSuffix().getValue()));
		else
			googleName.setNameSuffix(null);

		return googleName;
	}
	
	private static List<PhoneNumber> wrappMyPhoneNunbers(List<myContacts.PhoneNumber> phoneNumbers) {

		List<PhoneNumber> googlePhoneNumbers = new ArrayList<PhoneNumber>();

		for (myContacts.PhoneNumber phoneNumber : phoneNumbers) {

			PhoneNumber googlePhoneNumber = new PhoneNumber();

			googlePhoneNumber.setLabel(phoneNumber.getLabel());
			googlePhoneNumber.setPhoneNumber(phoneNumber.getPhoneNumber());
			googlePhoneNumber.setPrimary(phoneNumber.isPrimary());
			googlePhoneNumber.setRel(phoneNumber.getRel());
			googlePhoneNumber.setUri(phoneNumber.getUri());
			
			googlePhoneNumbers.add(googlePhoneNumber);
		}

		return googlePhoneNumbers;
	}
	
	private static List<StructuredPostalAddress> wrappMyStructuredPostalAddresses(List<myContacts.StructuredPostalAddress> structuredPostalAddresses) {

		List<StructuredPostalAddress> googleStructuredPostalAddresses = new ArrayList<StructuredPostalAddress>();

		for (myContacts.StructuredPostalAddress structuredPostalAddress : structuredPostalAddresses) {

			StructuredPostalAddress googleStructuredPostalAddress = new StructuredPostalAddress();

			if(structuredPostalAddress.getAgent()!=null)
				googleStructuredPostalAddress.setAgent(new com.google.gdata.data.extensions.Agent(structuredPostalAddress.getAgent().getValue()));
			else
				googleStructuredPostalAddress.setAgent(null);
			
			if(structuredPostalAddress.getCity()!=null)
				googleStructuredPostalAddress.setCity(new com.google.gdata.data.extensions.City(structuredPostalAddress.getCity().getValue()));
			else
				googleStructuredPostalAddress.setCity(null);
			
			if(structuredPostalAddress.getCountry()!=null)
				googleStructuredPostalAddress.setCountry(new com.google.gdata.data.extensions.Country(structuredPostalAddress.getCountry().getCode(), structuredPostalAddress.getCountry().getValue()));
			else
				googleStructuredPostalAddress.setCountry(null);
			
			if(googleStructuredPostalAddress.getFormattedAddress()!=null)
				googleStructuredPostalAddress.setFormattedAddress(new com.google.gdata.data.extensions.FormattedAddress(structuredPostalAddress.getFormattedAddress().getValue()));
			else
				googleStructuredPostalAddress.setFormattedAddress(null);
			
			if(structuredPostalAddress.getHousename()!=null)
				googleStructuredPostalAddress.setHousename(new HouseName(structuredPostalAddress.getHousename().getValue()));
			else
				googleStructuredPostalAddress.setHousename(null);
			
			googleStructuredPostalAddress.setLabel(structuredPostalAddress.getLabel());
			
			googleStructuredPostalAddress.setMailClass(structuredPostalAddress.getMailClass());
			
			if(structuredPostalAddress.getNeighborhood()!=null)
				googleStructuredPostalAddress.setNeighborhood(new com.google.gdata.data.extensions.Neighborhood(structuredPostalAddress.getNeighborhood().getValue()));
			else
				googleStructuredPostalAddress.setNeighborhood(null);
			
			if(structuredPostalAddress.getPobox()!=null)
				googleStructuredPostalAddress.setPobox(new com.google.gdata.data.extensions.PoBox(structuredPostalAddress.getPobox().getValue()));
			else
				googleStructuredPostalAddress.setPobox(null);
			
			if(structuredPostalAddress.getPostcode()!=null)
				googleStructuredPostalAddress.setPostcode(new com.google.gdata.data.extensions.PostCode(structuredPostalAddress.getPostcode().getValue()));
			else
				googleStructuredPostalAddress.setPostcode(null);
			
			googleStructuredPostalAddress.setPrimary(structuredPostalAddress.isPrimary());
			
			if(structuredPostalAddress.getRegion()!=null)
				googleStructuredPostalAddress.setRegion(new com.google.gdata.data.extensions.Region(structuredPostalAddress.getRegion().getValue()));
			else
				googleStructuredPostalAddress.setRegion(null);
			
			googleStructuredPostalAddress.setRel(structuredPostalAddress.getRel());
			
			if(structuredPostalAddress.getStreet()!=null)
				googleStructuredPostalAddress.setStreet(new com.google.gdata.data.extensions.Street(structuredPostalAddress.getStreet().getValue()));
			else
				googleStructuredPostalAddress.setStreet(null);
			
			if(structuredPostalAddress.getSubregion()!=null)
				googleStructuredPostalAddress.setSubregion(new com.google.gdata.data.extensions.Subregion(structuredPostalAddress.getSubregion().getValue()));
			else
				googleStructuredPostalAddress.setSubregion(null);
			
			googleStructuredPostalAddress.setUsage(structuredPostalAddress.getUsage());
			
			googleStructuredPostalAddresses.add(googleStructuredPostalAddress);			
		}

		return googleStructuredPostalAddresses;
	}
	
}
